package main

//测试用的main函数 与Flag包中一样
import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"os"
)

func Cat(r *bufio.Reader) {
	for {
		buf, err := r.ReadString('\n')
		if err == io.EOF {
			fmt.Fprintf(os.Stdout, "%s", buf)
			break
		}
		fmt.Fprintf(os.Stdout, "%s", buf)
	}
}
func main() {
	flag.Parse()
	fmt.Println(flag.Args())
	if flag.NArg() == 0 { //如果没有其他参数就会一直阻塞在这里
		Cat(bufio.NewReader(os.Stdin))
	}
	fmt.Println("传入参数就会打印该语句 否则一直阻塞")
	for i := 0; i < flag.NArg(); i++ {
		f, err := os.Open(flag.Arg(i))
		if err != nil {
			fmt.Fprintf(os.Stdout, "reading from %s failed, err:%v\n", flag.Arg(i), err)
			continue
		}
		Cat(bufio.NewReader(f))
	}
}
